package com.mmd.parkingspringboot.websocket;

import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RestController;

import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.concurrent.ConcurrentHashMap;
@RestController
@ServerEndpoint("/websocket")
@Component
public class WebSocketServer {

    /**
     * 静态变量，用来记录当前在线连接数。应该把它设计成线程安全的。
     */
    private static int onlineCount = 1000;
    /**
     * concurrent包的线程安全Set，用来存放每个客户端对应的MyWebSocket对象。
     */
    private static ConcurrentHashMap<Integer, WebSocketServer> webSocketMap = new ConcurrentHashMap<>();
    private static HashMap<Integer, String> dataMap = new HashMap<>();
    /**
     * 与某个客户端的连接会话，需要通过它来给客户端发送数据
     */
    private Session session;

    private static Session adminSession;

    /**
     * 连接建立成功调用的方法
     */
    @OnOpen
    public void onOpen(Session session) {
        this.session = session;
        if (webSocketMap.containsKey(onlineCount)) {
            webSocketMap.remove(onlineCount);
            webSocketMap.put(onlineCount, this);
            //加入set中
        }else {
            webSocketMap.put(onlineCount, this);
            //加入set中
            addOnlineCount();
            //在线数加1
        }
        System.out.println("连接成功");
        try {

        } catch (Exception e) {
        }
    }

    /**
     * 连接关闭调用的方法
     */
    @OnClose
    public void onClose() {
        if (webSocketMap.containsKey(onlineCount)) {
            webSocketMap.remove(onlineCount);
            //从set中删除
            subOnlineCount();
        }
    }

    @OnMessage
    public void onMessage(Session session, String message) throws IOException {
        System.out.println("收到" + message);
        if("admin".equals(message)){
            adminSession = this.session;
        }else{
            sendMessage(message);
        }
    }

    /**
     * @param session
     * @param error
     */
    @OnError
    public void onError(Session session, Throwable error) {
        error.printStackTrace();
    }

    /**
     * 实现服务器主动推送
     */
    public void sendMessage(String message) throws IOException {
        System.out.println("发送" + message);
        if(adminSession != null){
            adminSession.getBasicRemote().sendText("新注册的用户：" + message);
        }
    }

    /**
     * 发送自定义消息
     */
    public static void sendInfo(String message, @PathParam("userId") String userId) throws IOException {

    }

    /**
     * 获取当前在线人数
     *
     * @return
     */
    public static synchronized int getOnlineCount() {
        return onlineCount;
    }

    /**
     * 添加人数
     */
    public static synchronized void addOnlineCount() {
        WebSocketServer.onlineCount--;
    }

    /**
     * 减少人数
     */
    public static synchronized void subOnlineCount() {
        WebSocketServer.onlineCount++;
    }
}
